package com.onlineMIS.ORM.DAO.chainS.chainMgmt;


import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.apache.xmlbeans.impl.xb.xsdschema.RestrictionDocument.Restriction;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.onlineMIS.ORM.DAO.BaseDAO;
import com.onlineMIS.ORM.DAO.chainS.user.ChainStoreDaoImpl;
import com.onlineMIS.ORM.entity.chainS.chainMgmt.ChainProductShareGroup;
import com.onlineMIS.ORM.entity.chainS.chainMgmt.ChainStoreGroup;
import com.onlineMIS.ORM.entity.chainS.chainMgmt.ChainStoreGroupElement;
import com.onlineMIS.ORM.entity.chainS.user.ChainRoleType;
import com.onlineMIS.ORM.entity.chainS.user.ChainStore;
import com.onlineMIS.ORM.entity.chainS.user.ChainUserInfor;
import com.onlineMIS.ORM.entity.headQ.barcodeGentor.ProductBarcode;
import com.onlineMIS.common.Common_util;



@Repository
public class ChainProductShareGroupDaoImpl extends BaseDAO<ChainProductShareGroup> {
	@Autowired
	private ChainStoreDaoImpl chainStoreDaoImpl;
	
	public List<ChainStore> getChainGroupStoreList(int myChainId, ChainUserInfor loginUser, int accessLevel){
		List<ChainStore> chainStores = new ArrayList<ChainStore>();
		
		if (accessLevel == Common_util.CHAIN_ACCESS_LEVEL_4){
			chainStores = chainStoreDaoImpl.getStoreAndChildren(myChainId);
			return chainStores;
		} else {
			if (accessLevel == Common_util.CHAIN_ACCESS_LEVEL_3 || (accessLevel == Common_util.CHAIN_ACCESS_LEVEL_2  && loginUser.getRoleType().getChainRoleTypeId() == ChainRoleType.CHAIN_OWNER)){
				String hql = " FROM ChainProductShareGroup c WHERE c.chainGroup IN (SELECT b.chainGroup FROM ChainProductShareGroup b WHERE b.chainId = "+myChainId+")";

				
				List<ChainProductShareGroup> groups = this.getByHQL(hql, null, true);

				if (groups.size() >0){
					for (ChainProductShareGroup ele : groups){
						int chainId = ele.getChainId();
						ChainStore chainStore = chainStoreDaoImpl.get(chainId, true);
						if (chainStore != null)
							chainStores.add(chainStore);
					}
					return chainStores;
				}
			} 
	
			
			ChainStore chainStore = chainStoreDaoImpl.get(myChainId, true);
			if (chainStore != null)
				chainStores.add(chainStore);
	
			return chainStores;
		}
	}
	
	

}
